home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / components / calWcapCalendarModule.js < prev    next >
Encoding:
Text File  |  2007-05-23  |  10.1 KB  |  249 lines

  1. /* -*- Mode: javascript; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public
  6.  * License Version 1.1 (the "License"); you may not use this file
  7.  * except in compliance with the License. You may obtain a copy of
  8.  * the License at http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS
  11.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12.  * implied. See the License for the specific language governing
  13.  * rights and limitations under the License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is Sun Microsystems, Inc.
  18.  * Portions created by Sun Microsystems are Copyright (C) 2006 Sun
  19.  * Microsystems, Inc. All Rights Reserved.
  20.  *
  21.  * Original Author: Daniel Boelzle (daniel.boelzle@sun.com)
  22.  *
  23.  * Contributor(s):
  24.  *
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the NPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the NPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. //
  41. // init code for globals, prefs:
  42. //
  43.  
  44. // constants:
  45. const NS_OK = Components.results.NS_OK;
  46. const nsIException = Components.interfaces.nsIException;
  47. const calIWcapSession = Components.interfaces.calIWcapSession;
  48. const calIWcapCalendar = Components.interfaces.calIWcapCalendar;
  49. const calIWcapErrors = Components.interfaces.calIWcapErrors;
  50. const calICalendar = Components.interfaces.calICalendar;
  51. const calIItemBase = Components.interfaces.calIItemBase;
  52. const calIOperationListener = Components.interfaces.calIOperationListener;
  53.  
  54. // ctors:
  55. var CalEvent;
  56. var CalTodo;
  57. var CalDateTime;
  58. var CalDuration;
  59. var CalPeriod;
  60. var Timer;
  61.  
  62. // some string resources:
  63. var g_privateItemTitle;
  64. var g_confidentialItemTitle;
  65. var g_busyItemTitle;
  66. var g_busyPhantomItemUuidPrefix;
  67.  
  68. // global preferences:
  69. // caching: off|memory|storage:
  70. var CACHE = "off";
  71. // denotes where to host local storage calendar(s)
  72. var CACHE_DIR = null;
  73.  
  74. // caching the last data retrievals:
  75. var CACHE_LAST_RESULTS = 4;
  76. // timer secs for invalidation:
  77. var CACHE_LAST_RESULTS_INVALIDATE = 120;
  78.  
  79. // logging:
  80. var LOG_LEVEL = 0;
  81.  
  82. // whether alarms are turned on/off:
  83. var SUPPRESS_ALARMS = false;
  84.  
  85. var g_ioService = null;
  86. function getIoService() {
  87.     if (!g_ioService) {
  88.         g_ioService = Components.classes["@mozilla.org/network/io-service;1"]
  89.                       .getService(Components.interfaces.nsIIOService);
  90.     }
  91.     return g_ioService;
  92. }
  93.  
  94. function initWcapProvider()
  95. {
  96.     try {
  97.         // xxx todo: hack
  98.         // the master password prompt is currently not guarded against
  99.         // multiple prompt; this initializes/raises the pw db at early stage.
  100.         var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"]
  101.                                         .getService(Components.interfaces.nsIPasswordManager);
  102.         var enumerator = passwordManager.enumerator;
  103.         if (enumerator.hasMoreElements())
  104.             enumerator.getNext(); // actually prompts...
  105.     }
  106.     catch (exc) {
  107.     }
  108.     
  109.     try {        
  110.         // ctors:
  111.         CalEvent = new Components.Constructor("@mozilla.org/calendar/event;1",
  112.                                               "calIEvent");
  113.         CalTodo = new Components.Constructor("@mozilla.org/calendar/todo;1",
  114.                                              "calITodo");
  115.         CalDateTime = new Components.Constructor("@mozilla.org/calendar/datetime;1",
  116.                                                  "calIDateTime");
  117.         CalDuration = new Components.Constructor("@mozilla.org/calendar/duration;1",
  118.                                                  "calIDuration");
  119.         CalPeriod = new Components.Constructor("@mozilla.org/calendar/period;1",
  120.                                                "calIPeriod");
  121.         Timer = new Components.Constructor("@mozilla.org/timer;1",
  122.                                            "nsITimer");
  123.         
  124.         initLogging();
  125.         
  126.         // some string resources:
  127.         g_privateItemTitle = getWcapBundle().GetStringFromName("privateItem.title.text");
  128.         g_confidentialItemTitle = getWcapBundle().GetStringFromName("confidentialItem.title.text");
  129.         g_busyItemTitle = getWcapBundle().GetStringFromName("busyItem.title.text");
  130.         g_busyPhantomItemUuidPrefix = ("PHANTOM_uuid" + getTime().icalString);
  131.         
  132.         SUPPRESS_ALARMS = getPref("calendar.wcap.suppress_alarms", true);
  133.         
  134.         CACHE_LAST_RESULTS = getPref("calendar.wcap.cache_last_results", 4);
  135.         CACHE_LAST_RESULTS_INVALIDATE = getPref("calendar.wcap.cache_last_results_invalidate", 120);
  136.         
  137.         // init cache dir directory:
  138.         CACHE = getPref("calendar.wcap.cache", "off");
  139.         if (CACHE == "storage") {
  140.             var cacheDir = null;
  141.             var sCacheDir = getPref("calendar.wcap.cache_dir", null);
  142.             if (sCacheDir != null) {
  143.                 cacheDir = Components.classes["@mozilla.org/file/local;1"]
  144.                            .createInstance(Components.interfaces.nsILocalFile);
  145.                 cacheDir.initWithPath( sCacheDir );
  146.             }
  147.             else { // not found: default to wcap/ directory in profile
  148.                 var dirService = Components.classes["@mozilla.org/file/directory_service;1"]
  149.                                            .getService(Components.interfaces.nsIProperties);
  150.                 cacheDir = dirService.get("ProfD", Components.interfaces.nsILocalFile);
  151.                 cacheDir.append("wcap");
  152.             }
  153.             CACHE_DIR = cacheDir;
  154.             log(CACHE_DIR.path, "cache dir");
  155.             if (!CACHE_DIR.exists()) {
  156.                 CACHE_DIR.create(Components.interfaces.nsIFile.DIRECTORY_TYPE,
  157.                                  0700 /* read, write, execute/search by owner */);
  158.             }
  159.         }
  160.     }
  161.     catch (exc) {
  162.         logError(exc, "error in init sequence");
  163.     }
  164. }
  165.  
  166. var calWcapCalendarFactory = { // nsIFactory:
  167.     lockFactory: function calWcapCalendarFactory_lockFactory(lock) {},
  168.     
  169.     createInstance: function calWcapCalendarFactory_createInstance(outer, iid) {
  170.         if (outer)
  171.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  172.         var session = new calWcapSession();
  173.         return session.defaultCalendar.QueryInterface(iid);
  174.     }
  175. };
  176.  
  177. var calWcapCalendarModule = { // nsIModule:
  178.     
  179.     WcapCalendarInfo: {
  180.         classDescription: "Sun Java System Calendar Server WCAP Provider",
  181.         contractID: "@mozilla.org/calendar/calendar;1?type=wcap",
  182.         classID: Components.ID("{CF4D93E5-AF79-451a-95F3-109055B32EF0}")
  183.     },
  184.     
  185.     WcapSessionInfo: {
  186.         classDescription: "Sun Java System Calendar Server WCAP Session",
  187.         contractID: "@mozilla.org/calendar/wcap/session;1",
  188.         classID: Components.ID("{CBF803FD-4469-4999-AE39-367AF1C7B077}")
  189.     },
  190.     
  191.     registerSelf: function calWcapCalendarModule_registerSelf(compMgr, fileSpec, location, type)
  192.     {
  193.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  194.         compMgr.registerFactoryLocation(this.WcapCalendarInfo.classID,
  195.                                         this.WcapCalendarInfo.classDescription,
  196.                                         this.WcapCalendarInfo.contractID,
  197.                                         fileSpec, location, type);
  198.         compMgr.registerFactoryLocation(this.WcapSessionInfo.classID,
  199.                                         this.WcapSessionInfo.classDescription,
  200.                                         this.WcapSessionInfo.contractID,
  201.                                         fileSpec, location, type);
  202.     },
  203.     
  204.     unregisterSelf: function calWcapCalendarModule_unregisterSelf(compMgr, fileSpec, location)
  205.     {
  206.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  207.         compMgr.unregisterFactoryLocation(this.WcapCalendarInfo.classID, fileSpec);
  208.         compMgr.unregisterFactoryLocation(this.WcapSessionInfo.classID, fileSpec);
  209.     },
  210.     
  211.     m_scriptsLoaded: false,
  212.     getClassObject: function calWcapCalendarModule_getClassObject(compMgr, cid, iid)
  213.     {
  214.         if (!this.m_scriptsLoaded) {
  215.             // loading extra scripts from ../js:
  216.             const scripts = [ "calWcapUtils.js", "calWcapErrors.js",
  217.                               "calWcapRequest.js", "calWcapSession.js",
  218.                               "calWcapCalendar.js", "calWcapCalendarItems.js" ];
  219.             var scriptLoader =
  220.                 Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  221.                           .createInstance(Components.interfaces.mozIJSSubScriptLoader);
  222.             var ioService = getIoService();
  223.             var baseDir = __LOCATION__.parent.parent;
  224.             baseDir.append("js");
  225.             for each (var script in scripts) {
  226.                 var scriptFile = baseDir.clone();
  227.                 scriptFile.append(script);
  228.                 scriptLoader.loadSubScript(ioService.newFileURI(scriptFile).spec, null);
  229.             }
  230.             initWcapProvider();
  231.             this.m_scriptsLoaded = true;
  232.         }
  233.         
  234.         if (!iid.equals(Components.interfaces.nsIFactory))
  235.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  236.         if (!cid.equals(calWcapCalendar.prototype.classID))
  237.             throw Components.results.NS_ERROR_NO_INTERFACE;
  238.         return calWcapCalendarFactory;
  239.     },
  240.     
  241.     canUnload: function calWcapCalendarModule_canUnload(compMgr) { return true; }
  242. };
  243.  
  244. /** module export */
  245. function NSGetModule(compMgr, fileSpec) {
  246.     return calWcapCalendarModule;
  247. }
  248.  
  249.